<?php

namespace App\Models;

use App\Http\Constants\CoinsExceptionConstants;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class CoinsExceptionRecord extends Model
{
    use HasFactory;

    protected $table = 'life_coins_exception_record';

    protected $appends = ['status_name', 'coins_type_name'];

    protected $fillable = [
        'id',
        'ordersn',
        'order_amount',
        'member_id',
        'coins_id',
        'coins_type',
        'account_type',
        'change_amount',
        'exception_cause',
        'remark',
        'status',
        'operate',
        'operate_at',
        'auditor',
        'auditor_at',
        'execute',
        'execute_at',
        'created_at',
        'updated_at',
    ];

    protected $casts = [
        'ordersn' => 'string',
    ];

    public function member()
    {
        return $this->belongsTo('App\Models\Member', 'member_id', 'id');
    }

    public function getStatusNameAttribute()
    {
        $attributes = $this->attributes;
        $value = array_key_exists('status', $attributes) ? $attributes['status'] : null;
        if (is_null($value)) return null;
        $map = array_column(CoinsExceptionConstants::STATUS_TYPE, 'label', 'value');
        return array_key_exists($value, $map) ? $map[$value] : null;
    }

    public function getCoinsTypeNameAttribute()
    {
        return $this->attributes['coins_type'];
    }


}
